#include "panel.h"


sPanel::sPanel()
	:
x(0),y(0),width(0),height(0),r(128),g(128),b(128),a(200)
,onclick(0)
,onmousemove(0)
,onmousedown(0)
,onmouseup(0)
,onmouseenter(0)
,onmouseleave(0)

,childs(0)
,visible(1)
,nav(0)
{
	r = active_gui_engine->panel_r;
	g = active_gui_engine->panel_g;
	b = active_gui_engine->panel_b;
	a = 1.0f;

	nr = r;
	ng = g;
	nb = b;
	na = 1.0f;

	isaction = 0;
	isselectably = 0;
	sel = 0;
}

sPanel::~sPanel()
{
	if(childs !=0)delete childs;
}

void sPanel::SetBounds(int x_, int y_, int width_, int height_)
{
	x=x_;
	y=y_;
	width = width_;
	height = height_;

}

void sPanel::SetColor(float r_,float g_,float b_)
{
	r = r_;
	g = g_;
	b = b_;

	if(dynamic_cast<sPanel*>(this))
	{
		nr = r;
		ng = g;
		nb = b;
	}
}

void sPanel::SetColor(int r_,int g_,int b_)
{
	r = r_ / 255.0f;
	g = g_ / 255.0f;
	b = b_ / 255.0f;

	if(dynamic_cast<sPanel*>(this))
	{
		nr = r;
		ng = g;
		nb = b;
	}
}

void sPanel::SetAlpha(float alpha)
{
	a = alpha;
}

void sPanel::SetAlpha(int alpha)
{
	a = alpha / 255.0f;
}

void sPanel::Draw()
{
	int rx = x + sGUI::cx;
	int ry = y + sGUI::cy;

	if(nav)
		glColor4f(nr*1.1f,ng*1.1f,nb*1.1f,na);
	else
		glColor4f(r*0.8f,g*0.8f,b*0.8f,a);

	RoundRect(rx,ry,width,height,8);

	if(nav)
		glColor4f(nr*1.2f,ng*1.2f,nb*1.2f,na);
	else
		glColor4f(r*0.9f,g*0.9f,b*0.9f,a);

	RoundRect(rx+1,ry+1,width-2,height-2,8);

	if(nav)
		glColor4f(nr,ng,nb,na);
	else
		glColor4f(r,g,b,a);

	RoundRect(rx+2,ry+2,width-4,height-4,8);
}

void sPanel::OnClick(sEvent ev)
{
	onclick = ev;
}

void sPanel::OnMouseDown(sEvent ev)
{
	onmousedown = ev;
}

void sPanel::OnMouseUp(sEvent ev)
{
	onmouseup = ev;
}

void sPanel::OnMouseMove(sEvent ev)
{
	onmousemove = ev;
}

void sPanel::OnMouseEnter(sEvent ev)
{
	onmouseenter = ev;
}

void sPanel::OnMouseLeave(sEvent ev)
{
	onmouseleave = ev;
}

void sPanel::Show()
{
	visible = 1;
}
void sPanel::Hide()
{
	visible = 0;
}

void sPanel::Process()
{
	if(visible)
	{
		clicked = 0;

		Draw();

		if(isselectably)
		{
			if(MouseInRect(x + sGUI::cx,y + sGUI::cy,width - 2,height - 2))
			{
				if(nav==0)
				{
					#ifdef SIGEL_SOUND
						active_gui_engine->PlayNavSound();
					#endif

					if(onmouseenter!=0)
					{
						onmouseenter(this);
					}
				}

				nav = 1;

				if(MouseDown(1))
				{
					if(onmousedown!=0)
					{
						onmousedown(this);
					}
					if(md==0)
					{
						#ifdef SIGEL_SOUND
							active_gui_engine->PlayClickSound();
						#endif
						md = 1;
					}
				}
				else
				{
					if(md==1)
					{
						clicked = 1;
						if(onclick!=0)
						{
							onclick(this);
						}
					}
					md = 0;
				}
			}
			else
			{
				if(nav==0)
				{
					if(onmouseleave!=0)
					{
						onmouseleave(this);
					}
				}

				nav = 0;
				md = -1;
			}
		}

		// childs processing
		sGUI::cx += x;
		sGUI::cy += y;

		if(childs!=0)
		{
			std::for_each(childs->begin(),childs->end(),std::mem_fun(&sPanel::Process));
		}

		sGUI::cx -= x;
		sGUI::cy -= y;

	}
}

void sPanel::SetParent(sPanel *parent_)
{

	parent = parent_;
	if(parent != 0)
	{
		if(parent->childs == 0)
		{
			parent->childs = new sPanelList;
		}
		parent->childs->push_back(this);
		sGUI::AllObjects.remove(this);
	}
	else
	{
		if(parent!=0)
		{
			parent->childs->remove(this);
			sGUI::AllObjects.push_back(this);

			parent = 0;
		}
	}
}

int sPanel::GetX()
{
	return x;
}
int sPanel::GetY()
{
	return y;
}
int sPanel::GetWidth()
{
	return width;
}
int sPanel::GetHeight()
{
	return height;
}

void sPanel::SetPosition(int x_,int y_)
{
	x = x_;
	y = y_;
}

void sPanel::SetSize(int width_, int height_)
{
	width = width_;
	height = height_;
}

int sPanel::GetStatus(int s)
{
	switch(s)
	{
	case ST_CLICK:
		return clicked;
	default:
		return 0;
	}
}